package org.particles.gui;

import java.awt.event.ActionEvent;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.particles.domain.sim.SimulationCase;

/**
 * action by selecting a simulation in simulation list.
 */
public class SimSelectAction extends AbstractSimAction implements ListSelectionListener {
	private static final long serialVersionUID = 5609164300588706136L;
	private SimCtrlPanel controlPanel;
	
	@Override
	public void valueChanged(ListSelectionEvent evt) {
		if(evt.getValueIsAdjusting() || !isEnabled()) {
			return;
		}
		actionPerformed(GuiUtil.emptyActionEvent());
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		final boolean oldValue = getWorldTimeTask().isRunning();
		final boolean newValue = false;
		getWorldTimeTask().setRunning(newValue);
		if(oldValue != newValue) {
			firePropertyChange(GuiUtil.PROP_TASKRUN, oldValue, newValue);
		}
		getWorldTimeTask().setRunning(newValue);
		final SimulationCase simulation = controlPanel.getCurrentSimulation();
		controlPanel.recreatePanel(simulation);
		if(simulation.mustRecreate()) {
			getWorldPanel().setWorld(simulation.createWorld());
		}
		else {
			getWorldPanel().setWorld(simulation.getWorld());
		}
		getWorldPanel().revalidate();
		getWorldPanel().repaint();
	}

	public void setControlPanel(SimCtrlPanel controlPanel) {
		this.controlPanel = controlPanel;
	}

}
